package com.bdmc.smokesensor.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.bdmc.smokesensor.Utils.Msg;
import com.bdmc.smokesensor.Utils.ResultUtil;
import com.bdmc.smokesensor.entity.Rbac_Role;
import com.bdmc.smokesensor.entity.Sys_Log;
import com.bdmc.smokesensor.models.zTreeList;
import com.bdmc.smokesensor.services.Rbac_RoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/role",method=RequestMethod.POST)
public class Rbac_RoleController extends BaseController{

    @Autowired
    private Rbac_RoleService _roleSvs;

    @RequestMapping(value = "/list")
    public Msg list(@RequestBody Map<String, Object> reqMap) {
        int dep_id = Integer.valueOf(request.getAttribute("dep_id").toString());
        return ResultUtil.success(_roleSvs.findByTreeRoot(dep_id));
    }

    @RequestMapping(value = "/combo")
    public Msg GetComboList() {
        int dep_id = Integer.valueOf(request.getAttribute("dep_id").toString());
        List<Rbac_Role> list = _roleSvs.findByTreeRoot(dep_id);
        return ResultUtil.success(list);
    }

    @RequestMapping(value = "/list_page")
    public Msg list_Sys_Role(@RequestBody Map<String, Integer> reqMap) {
        int page =reqMap.get("page");
        int rows =reqMap.get("rows");
        Map<String, Object> map = _roleSvs.findBySqlPageable(page, rows);
        return ResultUtil.success(map);
    }

    @RequestMapping(value = "/save")
    public Msg save(@RequestBody Rbac_Role role) {
        String action = role.getId()>0?"修改:":"新增:";
        int dep_id = Integer.valueOf(request.getAttribute("dep_id").toString());
        role.setDepartMentId(dep_id);
        Rbac_Role f = _roleSvs.save(role);
        if (f != null && f.getId() > 0) {
            Sys_Log log = new Sys_Log(getLoginName(), getLocalIp() , "角色管理", action+f.getRoleName());
            _logSvs.save(log);
            return ResultUtil.success();
        } else {
            return ResultUtil.error(-1, "操作失败，请联系管理员。");
        }

    }

    @RequestMapping(value = "/delete")
    public Msg delete(@RequestBody Map<String,Integer> map) {
        int id =Integer.valueOf(map.get("id"));
        _roleSvs.deleteById(id);
        Sys_Log log = new Sys_Log(getLoginName(), getLocalIp() , "角色管理", "删除角色："+id);
            _logSvs.save(log);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/tree")
    public Msg roleTree(@RequestBody Map<String,Integer> map) {
        int id =Integer.valueOf(map.get("id"));
        List<zTreeList> zTree = _roleSvs.GetzTreeListByRoleId(id);//页面权限
        List<zTreeList> actions =_roleSvs.GetActionListByRoleId(id) ;//按钮权限
        List<zTreeList> rootTreeList = zTree.stream().filter(g -> g.Pid == 0).collect(Collectors.toList());
        for (zTreeList f : rootTreeList) {
            CreateRoleTree(f, zTree,actions);
        }
        return ResultUtil.success(rootTreeList);
    }

    @RequestMapping(value = "/set_tree")
    public Msg setRoleTree(@RequestBody Map<String,Object> map) {
        int roleid=Integer.valueOf(map.get("roleid").toString()) ;
        String function = map.get("function").toString();
        String action = map.get("action").toString();
        Rbac_Role role= _roleSvs.findOneById(roleid);
        if(role!=null){
            role.setStrFuction(function);
            role.setStrAction(action);
            _roleSvs.save(role);
            return ResultUtil.success();
        }else{
            return ResultUtil.error(-1, "角色不存在");
        }
    }

    private void CreateRoleTree(zTreeList rootTree, List<zTreeList> lstGroup,List<zTreeList> actions) {
        //zTreeList aaa=new zTreeList();
        List<zTreeList> childTree = new ArrayList<zTreeList>();
        List<zTreeList> childList = lstGroup.stream().filter(g -> g.Pid == rootTree.id && g.funtype == 0)
                .collect(Collectors.toList());
        if (childList.size() > 0) {
            for (zTreeList f : childList) {
                List<zTreeList> childActions=new ArrayList<zTreeList>();  
                for(zTreeList a : actions){
                    if(a.Pid==f.id){
                        f.checked=false;
                        childActions.add(a);
                    }
                }
                f.setChildren(childActions);
                childTree.add(f);
            }
            rootTree.children = childTree;
        } else {
            rootTree.children = new ArrayList<zTreeList>();
        }
    }
}
